package com.cypher.encryptionanddecryption.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.dao.CommunicationDao;
import com.cypher.encryptionanddecryption.util.DbUtils;

import org.apache.commons.lang3.StringUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通信信息Dao实现类
 * @author: 古霸天
 * @date: 2020/11/27
 */
public class CommunicationDaoImpl implements CommunicationDao {
    @Override
    public List<CommunicationBean> listCommunicationBean(int protocolId) {

        String sql = "SELECT * FROM tb_param_manager WHERE PROTOCOL_ID=? ORDER BY ID ASC";

        Map<Integer, Object> params = new HashMap<Integer, Object>();
        params.put(1, protocolId);

        return getBeans(sql, params);
    }

    @Override
    public List<CommunicationBean> listCommunicationBean(int protocolId, String condition) {
        String sql = "SELECT * FROM tb_param_manager WHERE PROTOCOL_ID=? AND"
                + "(NAME LIKE ? OR HEX_CODE LIKE ?) ORDER BY ID ASC";

        Map<Integer, Object> params = new HashMap();
        params.put(1, protocolId);
        params.put(2, "%" + condition + "%");
        params.put(3, "%" + condition + "%");

        return getBeans(sql, params);
    }

    private  List<CommunicationBean> getBeans(String sql, Map<Integer, Object> params) {
        List<CommunicationBean> beans = new ArrayList();
        PreparedStatement ps = DbUtils.getPreparedStatement(sql, params);
        if (ps == null) return beans;

        try {
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                beans.add(wrap(rs));
            }
            DbUtils.closeConn(ps, rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return beans;
    }

    public CommunicationBean wrap(ResultSet rs) throws SQLException {
        CommunicationBean bean = new CommunicationBean();
        bean.setId(rs.getInt("ID"));
        bean.setProtocolId(CommunicationBean.ProtocolType.values()[rs.getInt("PROTOCOL_ID")]);
        bean.setHexCode(rs.getString("HEX_CODE"));
        bean.setName(rs.getString("NAME"));
        bean.setRwType(CommunicationBean.RWType.values()[rs.getInt("RW_TYPE")]);
        bean.setEditType(CommunicationBean.EditType.values()[rs.getInt("EDIT_TYPE")]);
        bean.setSpecial(rs.getString("SPECIAL"));
        bean.setCreateData(rs.getDate("CREATE_DATE"));
        bean.setLastModifiedDate(rs.getDate("LAST_MODIFIED_DATE"));
        bean.setField1(rs.getString("FIELD1"));
        bean.setField2(rs.getString("FIELD2"));
        bean.setField3(rs.getString("FIELD3"));
        bean.setField4(rs.getString("FIELD4"));
        bean.setInputType(rs.getString("INPUT_TYPE"));
        // 扩展属性转换为Map
        String attrStr = rs.getString("ATTR");
        if (!StringUtils.isEmpty(attrStr)) {
            bean.setAttr(JSONObject.parseObject(attrStr, Map.class));
        }
        return bean;
    }
}
